package com.qyh.lucas.utils;

import lombok.val;


public class ReflectionUtils {
    public static void setFieldValue(Object obj, String fieldName, Object value) {
        val field = org.springframework.util.ReflectionUtils.findField(obj.getClass(), fieldName);
        if (field == null) {
            throw new IllegalArgumentException("fieldName: " + fieldName + "不存在！");
        }
        field.setAccessible(true);
        try {
            field.set(obj, value);
        } catch (Exception e) {
            throw new IllegalArgumentException("非法参数", e);
        }
    }

    public static <T> Object getFieldValue(T t, String fieldName) {
        val field = org.springframework.util.ReflectionUtils.findField(t.getClass(), fieldName);
        if (field == null) {
            throw new IllegalArgumentException("fieldName" + fieldName + "不存在！");
        }
        field.setAccessible(true);
        try {
            return field.get(t);
        } catch (Exception e) {
            throw new IllegalArgumentException("非法参数", e);
        }
    }
}

